package com.anlogic.sdk.linker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Property implements IProperty {
	private String name;

	private List<IProperty> children;

	private IProperty parent;

	private int type;

	private String defaultValue;

	private String value;

	private String[] valueOptions;

	private String desc;

	private boolean isCategory;

	private boolean editable;

	private int state;

	private boolean permit;

	public Property(String name) {
		setName(name);
		this.editable = true;
		this.isCategory = false;
		this.permit = true;
		this.children = new ArrayList<IProperty>();
	}

	public int getType() {
		return this.type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setTypeFromString(String type) {
		switch (type) {
		case TYPE_BOOLEAN_NAME:
			this.type = TYPE_BOOLEAN;
			break;
		case TYPE_INTEGER_NAME:
			this.type = TYPE_INTEGER;
			break;
		case TYPE_ARRAY_NAME:
			this.type = TYPE_ARRAY;
			break;
		case TYPE_STRING_NAME:
			this.type = TYPE_STRING;
			break;
		}
	}
	
	public String getTypeAsString() {
		switch (this.type) {
		case TYPE_BOOLEAN:
			return TYPE_BOOLEAN_NAME;
		case TYPE_INTEGER:
			return TYPE_INTEGER_NAME;
		case TYPE_STRING:
			return TYPE_STRING_NAME;
		case TYPE_ARRAY:
			return TYPE_ARRAY_NAME;
		}
		return "";
	}
	
	public String getDefaultValue() {
		return this.defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getValue() {
		return this.value;
	}

	public int getIndexOfValue() {
		for (int i = 0; i < this.valueOptions.length; i++) {
			if (this.valueOptions[i].equals(this.value))
				return i;
		}
		return -1;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDesc() {
		return this.desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public boolean isCategory() {
		return this.isCategory;
	}

	public void setCategory(boolean isCategory) {
		this.isCategory = isCategory;
	}

	public void addChild(IProperty c) {
		this.children.add(c);
		c.setParent(this);
	}

	public void removeChild(IProperty c) {
		this.children.remove(c);
		c.setParent(null);
	}

	public List<IProperty> getChildren() {
		return this.children;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setParent(IProperty parent) {
		this.parent = parent;
	}

	public IProperty getParent() {
		return this.parent;
	}

	public void setValueOptions(String[] valueOptions) {
		this.valueOptions = valueOptions;
	}

	public String[] getValueOptions() {
		return this.valueOptions;
	}

	public boolean getIsEditable() {
		return this.editable;
	}

	public void setIsEditable(boolean edit) {
		this.editable = edit;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getState() {
		return this.state;
	}

	public boolean getPermit() {
		return this.permit;
	}

	public void setPermit(boolean permit) {
		this.permit = permit;
	}

	public String toString() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Name", this.name);
		map.put("Type", getTypeAsString());
		map.put("Value", this.value);
		return map.toString();
	}
}
